package com.qf.qftenantmanager.web.controller;

import com.qf.common.bean.R;
import com.qf.qftenantmanager.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/tenant/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     *
     * @param tenantId
     * @param deleted 是否删除。0-未删除，1-删除
     * @return
     */
    @GetMapping("/count/{tenantId}/{deleted}")
    public R count(@PathVariable("tenantId") Long tenantId,@PathVariable("deleted") int deleted){
        return deviceService.countByTenantId(tenantId,deleted);
    }

    @GetMapping("/listByTenant/{tenantId}")
    public R listByTenant(@PathVariable("tenantId") Long tenantId) throws ExecutionException, InterruptedException {
        return deviceService.listByTenant(tenantId);
    }

    @GetMapping("/isOnline/{productKey}/{deviceKey}")
    public R isOnline(@PathVariable("productKey") String productKey,@PathVariable("deviceKey")String deviceKey){
        //todo: 返回设备状态，后续根据心跳数据获取
        Random random = new Random();
        return R.ok(random.nextInt(10)>5?true:false);
    }


    @GetMapping("/get/{deviceId}")
    public R get(@PathVariable("deviceId") Long deviceId){
        return deviceService.get(deviceId);
    }
}
